package cn.bone.common.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by superbone on 2017/3/16.
 */
public class EncodeUtil {

    /**
     * md5加密
     */
    public static String md5(String str){

        String result = null;
        try {
            result = DigestUtils.md5Hex(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * base64加密 返回字节数组
     */
    public static byte[] base64Encode(String str){

        try {
            return Base64.encodeBase64(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base64加密 返回字符串
     */
    public static String base64Encode2String(String str){

        try {
            return Base64.encodeBase64String(str.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * base64解密 参数为字节数组
     */
    public static String base64Decode(byte[] bytes){

        byte[] bytes1 = Base64.decodeBase64(bytes);
        try {
            return new String(bytes1,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * base64解密 参数为字符串
     */
    public static String base64Decode(String str){

        try {
            byte[] bytes1 = Base64.decodeBase64(str.getBytes("UTF-8"));
            return new String(bytes1,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * AES加密
     */
    public static String aesEncode(String src,String key){
        String result = null;
        try {
            byte[] keyBytes = key.getBytes("UTF-8");
            SecretKey secretKeySpec = new SecretKeySpec(keyBytes,"AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
            byte[] bytes = cipher.doFinal(src.getBytes("UTF-8"));
            result = Base64.encodeBase64String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * AES解密
     */
    public static String aesDecode(String src,String key){
        String result = null;
        try{
            byte[] keyBytes = key.getBytes("UTF-8");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes,"AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
            byte[] input = Base64.decodeBase64(src);
            byte[] bytes = cipher.doFinal(input);
            result = new String(bytes,"UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 加密
     * 1. URLEncode
     * 2. aes加密
     * 3. base64加密
     */
    public static String aesJiami(String src,String key){
        String result = null;
        try {
            String str = URLEncoder.encode(src,"UTF-8");
            byte[] keyBytes = key.getBytes("UTF-8");
            SecretKey secretKeySpec = new SecretKeySpec(keyBytes,"AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
            byte[] bytes = cipher.doFinal(str.getBytes("UTF-8"));
            result = Base64.encodeBase64String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 解密
     * 1. base64解密
     * 2. aes解密
     * 3. URLDecode
     */
    public static String aesJiemi(String src,String key){
        String result = null;
        try{
            byte[] keyBytes = key.getBytes("UTF-8");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes,"AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
            byte[] input = Base64.decodeBase64(src);
            byte[] bytes = cipher.doFinal(input);
            String str = new String(bytes,"UTF-8");
            result = URLDecoder.decode(str,"UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
